<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* 
*/
class Login extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();

        $this->load->database();
        $this->load->helper('url');

        $this->load->library('session');  
	}


	//登录
	public function login()
	{
		$islogin = $this->input->post('islogin',true);

		if (!empty($islogin))
		{
			$arr['username'] = $this->input->post('username',true);
			$arr['password'] = $this->input->post('password',true);

			//检查账号密码是否正确
			$this->load->model('User/LoginModel','login');
			$user = $this->login->user_login($arr);

			//账号密码正确则登录
			if ($user['admin_id'] > 0) 
			{
				//设置用户session
				$this->session->set_userdata('admin_id', $user['admin_id']);

				$data['status'] = 200;
				$data['message'] = '登录成功！';
			}
			else
			{
				$data['status'] = 400;
				$data['message'] = '账号或密码不正确！';
			}

			echo json_encode($data);exit;

		}
		else
		{
			if ($this->session->userdata('admin_id') > 0) 
			{
				//跳转到首页
				redirect(base_url('admin.php/frame/index'));

			}
			else
			{
				$this->load->view('login');
			}
		}
	}


	//退出登录
	public function sign_out()
	{
		$this->session->unset_userdata('admin_id');   //注销用户session

		redirect(base_url('admin.php/login/login'));
	}

	
}


?>